package com.example.bgmachine.bgmachine.sets;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import com.example.bgmachine.bgmachine.R;
import com.example.bgmachine.bgmachine.homes.HomePages;
import com.example.bgmachine.bgmachine.orders.OrderContentData;

import java.util.LinkedList;

public class EmptySetAdapter extends BaseAdapter {

    private Context mContext;
    private LinkedList<EmptySetData> mData = new LinkedList<EmptySetData>();

    public EmptySetAdapter(Context context, LinkedList<EmptySetData> list) {
        this.mContext = context;
        this.mData = list;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    // txt_pin txt_content
    @Override
    public View getView(int position, View convertViewtms, ViewGroup parent) {
        EmptySetAdapter.ViewHolder holder = null;
        if (convertViewtms == null) {
            View convertView = View.inflate(mContext, R.layout.frament_set_empty_list_item_content, null);
            convertViewtms= convertView;

            holder = new EmptySetAdapter.ViewHolder();
            holder.mEmptySetData = mData.get(position);
            holder.set_empty_item_name_textview = convertView.findViewById(R.id.set_empty_item_name_textview);
            holder.set_empty_item_pin_textview = convertView.findViewById(R.id.set_empty_item_pin_textview);
            holder.set_empty_item_pin_checkbox = convertView.findViewById(R.id.set_empty_item_pin_checkbox);
            holder.set_empty_item_pin_checkbox.setTag(holder);
            holder.set_empty_item_pin_checkbox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    EmptySetAdapter.ViewHolder vs =(EmptySetAdapter.ViewHolder)v.getTag();
                    boolean isSelect = vs.set_empty_item_pin_checkbox.isChecked();
                    vs.mEmptySetData.setSelectEd(isSelect);
                }
            });

            holder.set_empty_item_name_textview.setText(holder.mEmptySetData.getMenuName());
            holder.set_empty_item_pin_textview.setText( String.valueOf( holder.mEmptySetData.getMenuid() ) );

            holder.set_empty_item_pin_checkbox.setChecked(holder.mEmptySetData.isSelectEd());
            convertView.setTag(holder);
        } else {

            holder = (EmptySetAdapter.ViewHolder) convertViewtms.getTag();
            holder.mEmptySetData = mData.get(position);

            holder.set_empty_item_name_textview = convertViewtms.findViewById(R.id.set_empty_item_name_textview);
            holder.set_empty_item_pin_textview = convertViewtms.findViewById(R.id.set_empty_item_pin_textview);
            holder.set_empty_item_pin_checkbox = convertViewtms.findViewById(R.id.set_empty_item_pin_checkbox);
            holder.set_empty_item_pin_checkbox.setTag(holder);
            holder.set_empty_item_name_textview.setText(holder.mEmptySetData.getMenuName());
            holder.set_empty_item_pin_textview.setText( String.valueOf( holder.mEmptySetData.getMenuid() ) );

            holder.set_empty_item_pin_checkbox.setChecked(holder.mEmptySetData.isSelectEd());

        }

        return convertViewtms;
    }
    //添加一个元素
    public void add(EmptySetData data) {
        if (mData == null) {
            mData = new LinkedList<>();
        }
        mData.add(data);
        notifyDataSetChanged();
    }

    //往特定位置，添加一个元素
    public void add(int position, EmptySetData data) {
        if (mData == null) {
            mData = new LinkedList<>();
        }
        mData.add(position, data);
        notifyDataSetChanged();
    }

    public void remove(EmptySetData data) {
        if (mData != null) {
            mData.remove(data);
        }
        notifyDataSetChanged();
    }

    public void remove(int position) {
        if (mData != null) {
            mData.remove(position);
        }
        notifyDataSetChanged();
    }

    public void clear() {
        if (mData != null) {
            mData.clear();
        }
        notifyDataSetChanged();
    }


    private class ViewHolder {

        public  TextView set_empty_item_pin_textview = null;
        public  TextView set_empty_item_name_textview  = null;
        public CheckBox set_empty_item_pin_checkbox  = null;


        public EmptySetData mEmptySetData = null;

    }


}
